//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tmf\client\fn_ammoBoxExec.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
private ["_box_idx","_not_found","_box_marker","_box_array","_marker_name"];

_box_idx = 0;
_not_found = 0;
for "_box_idx" from 0 to 500 step 1 do {
	_box_marker = format["box_%1",_box_idx];
	if (IS_MARKER(_box_marker)) then {
		private ["_text_marker","_marker_array","_box","_box_type","_box_side","_box_extra","_box_pos","_regular_pos","_index"];
		_index = GVAR(AMMO_BOXES_INDEXER) find _box_marker;
		if (_index < 0) then {
			_text_marker = markerText _box_marker;
			_marker_array = [];
			if (!isnil "_text_marker") then {
				if (_text_marker != "") then {
					call compile format["if (typename %1 == typename []) then { _marker_array = %1; };",_text_marker];
				};
			};
			if (isnil "_marker_array") then {_marker_array = [];};
			if (typename _marker_array != typename []) then {_marker_array = [];};
			_box = objNull;
			_box_array = [];
			_box_type = tmf_ammobox_type;
			_box_side = toUpper(str(playerSide));
			_box_extra = [];
			_box_pos = (getMarkerPos _box_marker);
			_regular_pos = true;
			if (typename _marker_array == typename []) then {
				if (count _marker_array > 0) then { _box_side = toupper(_marker_array select 0);};
				if (count _marker_array > 1) then { _box_type = _marker_array select 1;};
				if (count _marker_array > 2) then { _box_extra = _marker_array select 2;};
				if (count _marker_array > 3) then { _box_pos = _marker_array select 3; _regular_pos = false;};
			};
			if (_box_type == "") then { _box_type = tmf_ammobox_type; };
			_index = count GVAR(AMMO_BOXES_INDEXER);
			// diag_log format["Ammo_Box_Fill: New box: %1", [_index, _box_marker, _marker_array]];
			GVAR(AMMO_BOXES_INDEXER) set [_index, _box_marker];
			_box = _box_type createVehicleLocal _box_pos;
			_box setDir (markerDir _box_marker);
			if (_regular_pos) then {
				_box setPos _box_pos;
			} else {
				_box setPosATL _box_pos;
			};
			_box allowDamage false;
			_box addEventHandler ["HandleDamage",{0}];
			_box setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
			_box_array set [0, _box];
			_box_array set [1, _box_type];
			_box_array set [2, _box_side];
			_box_array set [3, _box_extra];
			GVAR(AMMO_BOXES) set [_index, _box_array];
			[_box, _box_side,_box_extra] call FUNC(ammoBoxFill);
			
			_box setVariable ["Last_Box_Fill",time];
			_marker_name = format ["%1_local",_box_marker];
			[_marker_name, getMarkerPos _box_marker, "ICON", "ColorBlue", [0.6,0.6],"",0,"mil_pickup","",0.7] call TFUNC(createMarkerLocal);
			player reveal _box;
		} else {
			if (tmf_ammobox_refill) then {
				_box_array = GVAR(AMMO_BOXES) select _index;
				_box = _box_array select 0;
				_box_side = _box_array select 2;
				_box_extra = _box_array select 3;
				_last_fill = _box getVariable "Last_Box_Fill";
				if (isnil "_last_fill") then { _last_fill = 0; };
				if ((player distance _box) > 200 && (time - _last_fill) > 900) then {
					[_box, _box_side,_box_extra] call FUNC(ammoBoxFill);
					_box setVariable ["Last_Box_Fill",time];
				};
			};
		};
	} else {
		_not_found = _not_found + 1;
	};
	if (_not_found > 20) exitWith {};
};

for "_box_idx" from 0 to ((count GVAR(AMMO_BOXES_INDEXER)) - 1) step 1 do {
	_box_marker = GVAR(AMMO_BOXES_INDEXER) select _box_idx;
	if !(IS_MARKER(_box_marker)) then {
		_box_array = GVAR(AMMO_BOXES) select _box_idx;
		if (!(isNull(_box_array select 0))) then {
			deleteVehicle (_box_array select 0);
		};
		GVAR(AMMO_BOXES) set [_box_idx, "XxDeLxX"];
		GVAR(AMMO_BOXES_INDEXER) set [_box_idx, "XxDeLxX"];
		_marker_name = format ["%1_local",_box_marker];
		deleteMarkerLocal _marker_name;
		GVAR(AMMO_BOXES_INDEXER) = GVAR(AMMO_BOXES_INDEXER) - ["XxDeLxX"];
		GVAR(AMMO_BOXES) = GVAR(AMMO_BOXES) - ["XxDeLxX"];
	};
};
TRACE("End");